package com.kuaimacode.kframework.admin.services.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.admin.services.user.UserService;
import com.kuaimacode.kframework.admin.vo.request.SysMessageReq;
import com.kuaimacode.kframework.admin.vo.response.SysMessageResp;
import com.kuaimacode.kframework.component.AliPushService;
import com.kuaimacode.kframework.exception.ErrorEnum;
import com.kuaimacode.kframework.exception.ServerInternalErrorException;
import com.kuaimacode.kframework.mybatis.mapper.sys.SysMessageMapper;
import com.kuaimacode.kframework.mybatis.models.sys.SysMessage;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author qinyong
 * @Title:
 * @Package com.kuaimacode.kframework.api.service
 * @Description: created by IntelliJ IDEA
 * @date 2019-03-15 21:51
 */
@Service
public class SysMessageService extends ServiceImpl<SysMessageMapper, SysMessage> {

    @Autowired
    private UserService userService;
    @Autowired
    private AliPushService aliPushService;
    /**
     * 分页查找所有的数据
     *
     * @param pageNum  当前页码，从1开始
     * @param pageSize 一页显示多少行
     * @param searchMessage
     * @return
     */
    public PageInfo<SysMessageResp> findAll(int pageNum, int pageSize, SysMessage searchMessage) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper<SysMessage> queryWrapper = new QueryWrapper<>();
        Optional.ofNullable(searchMessage.getUserType())
                .ifPresent(userType -> queryWrapper.like("user_type", userType));
        Optional.ofNullable(searchMessage.getType())
                .ifPresent(type -> queryWrapper.eq("type", type));
        List<SysMessage> datas = baseMapper.selectList(queryWrapper);
        List<SysMessageResp> resps = new ArrayList<>();
        Optional.ofNullable(datas)
                .ifPresent(messages -> messages.forEach(message -> {
                    SysMessageResp messageResp = new SysMessageResp();
                    BeanUtils.copyProperties(message, messageResp);
                    resps.add(messageResp);
                }));

        List<User> users = Optional.ofNullable(userService.list()).orElse(new ArrayList<>());
        resps.forEach(sysMessageResp -> users.forEach(user -> {
            if (sysMessageResp.getUserId().equals(user.getId())) {
                sysMessageResp.setPhone(user.getPhone());
            }
        }));
        return new PageInfo<>(resps);
    }

    public void save(SysMessageReq messageReq) {
        //根据手机号查询用户
        User user = userService.selectByPhone(messageReq.getPhone());
        if (user == null) {
            throw new ServerInternalErrorException(ErrorEnum.USER_NOT_EXISTS);
        }

        SysMessage save = new SysMessage();
        BeanUtils.copyProperties(messageReq, save);
        save.setUserId(user.getId());
        Date now = new Date();
        save.setCreateAt(now);
        save.setUpdateAt(now);
        this.save(save);
        if (messageReq.getIsPush()) {
            aliPushService.pushByAlias(messageReq.getTitle(), messageReq.getContent(), user.getId(), null);
        }
    }

//    public void pushSystemUserMsg()
}
